package com.fenshi.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.fenshi.interactor.DamageOrderInteractor;
import com.fenshi.interactor.imp.DamageOrderInteractorImp;
import com.fenshi.manager.R;
import com.fenshi.manager.activity.backcar.BackActivity;
import com.fenshi.presenter.DamageOrderPresenter;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.CookicUntil;
import com.fenshi.until.PostUpLoadRequest;
import com.fenshi.view.DamageOrderView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/18.
 */
public class DamageOrderPresenterImp implements DamageOrderPresenter, RequestListener<JSONObject> {


    DamageOrderView view;
    DamageOrderInteractor interactor;
    Context context;
    Map<Integer,String> paths;
    String r;

    public DamageOrderPresenterImp(Context context){
        this.context = context;
        view = (DamageOrderView) context;
        interactor = new DamageOrderInteractorImp(context,this);
    }

    @Override
    public void upDate() {
        view.showLoading();
        paths = view.getFilePath();
        Task task = new Task();
        if (paths.size() == 0)
            ((Activity) context).finish();
        else
        {
            List<File> files = new ArrayList<>();

           for (Map.Entry<Integer,String> map :paths.entrySet())
           {

               File file = new File(map.getValue());
               files.add(file);
           }
            task.execute(files);
        }
    }

    @Override
    public void evidence() {
        String userId = CookicUntil.getUser().getUserId();
        String orderId = ((Activity) context).getIntent().getExtras().getString(CookicUntil.ORDER_ID);
        String decription = "";
        if (view.getDec()!=null  && view.getDec().length() > 0 ) {
            decription = view.getDec();
        }
        interactor.evidence(orderId,userId,r,decription);
    }



    @Override
    public void Success(String tag, JSONObject result) {
        view.dismissLoading();

        Intent intent = ((Activity) context).getIntent();
        intent.setClass(context,BackActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("pic",r);
        intent.putExtras(bundle);
        ((Activity) context).setResult(BackActivity.BACKCAR, intent);
        view.showInfo("上传成功");
        ((Activity) context).finish();
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoading();
        view.showInfo(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoading();
        view.showInfo(context.getString(R.string.system_error));
    }



    class Task extends AsyncTask<List<File>,Integer,List<String>>{

        PostUpLoadRequest request;

        public Task() {
            request = new PostUpLoadRequest();
        }

        @Override
        protected List<String> doInBackground(List<File>... params) {
            List<String> names =new ArrayList<>();
            List<File> f= params[0];
            for(File file :f) {
                String name = request.uploadFile(file,null);
                names.add(name);
            }
            return names;
        }

        @Override
        protected void onPostExecute(List<String> strings) {
            StringBuffer result = new StringBuffer();
            for (String s : strings) {
                result.append(s + ",");
            }
            r = result.substring(0,result.length()-1);
            if (r!=null && r.length()> 0)
                evidence();
        }
    }
}
